import Vue from 'vue'
import Router from 'vue-router'

const Shouye = () => import('@/shitu/Home/Shouye')
const Fenlei = () => import('@/shitu/Fenlei/Fenlei')
const Gouwuche = () => import('@/shitu/Gouwuche/Gouwuche')
const Wode = () => import('@/shitu/Wode/Wode')
const Detail  = () => import('@/shitu/detail/Detail')

Vue.use(Router)

const routes = [
  {
    path: '',
    redirect: '/shouye',
    meta: {title: "超级商城"},
  },
  { path: '/shouye',
    component: Shouye,
    meta: {title: "supermail"}
  },
  { path: '/fenlei',
    component: Fenlei,
    meta: {title: "分类"}
  },
  { path: '/gouwuche',
    component: Gouwuche,
    meta: {title: "购物街"}
  },
  { path: '/wode',
    component: Wode ,
    meta: {title: "我的"}
  },
  { path: '/detail/:iid',
    component: Detail ,
    meta: {title: "商品详情页"}
  }
]
const router = new Router({
  routes,
  mode: "history"
})
//  导航守卫 前置守卫beforeEach((to,from,next)=>{next()})跳转前回调
//  路由独享守卫beforeEnter,跟前置守卫类似，写到路由中
// 后置守卫afterEach((to,from)=>{})                                    跳转后回调
router.beforeEach((to,from,next) => {
  document.title = to.matched[0].meta.title
  next()
})
//  多次点击跳转同一个路由是不被允许的，解决方法
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}
const VueRouterReplace = Router.prototype.replace
Router.prototype.replace = function replace (to) {
  return VueRouterReplace.call(this, to).catch(err => err)
}
export default router
